//
//  AppDelegate.m
//  FinancialProduct
//
//  Created by Stella on 2017/8/24.
//  Copyright © 2017年 maq. All rights reserved.
//
#import "MyNavigationController.h"
#import "AppDelegate.h"
#import "HandleNetProduct.h"
#import "UIAlertView+ActionBlock.h"
#import "CKPuslichTool.h"
#import "LoginViewController.h"
#import "PicPassController.h"
#import "MSTabBarView.h"
#import "YKGuidePageController.h"
#import "Header.h"
#import "MSCustomTabBar.h"
#import "NSString+NN.h"
#import "SVProgressHUD.h"
#import "UIColor+NN.h"
#import "HandleNetLogin.h"
#import "UserModel.h"
#import "SVProgressHUD.h"
#import "SVProgressHUD_Extension.h"
@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    [self svPreferrenceConf];
    // 引导页
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    if (![DEF_PERSISTENT_GET_OBJECT(@"showGuideVersion") isEqualToString:version])
    {
        
        /** 加载启动图*/
        NSMutableArray *picArr = @[
                                   [UIImage imageNamed:@"loading1"],
                                   [UIImage imageNamed:@"loading2"],
                                   [UIImage imageNamed:@"loading3"]
                                   ].mutableCopy;
//        [[YKGuidePageController sharedGuideVC] fyhGuidePageWithPicArr:picArr];
        YKGuidePageController *guidePageVC = (YKGuidePageController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Main" viewID:@"YKGuidePageController"];
        guidePageVC.picArr=picArr;
        _window.rootViewController = guidePageVC;
        
    }else{
        NSString *oldPass=[[NSUserDefaults standardUserDefaults]objectForKey:@"PICPASSCODE"];
        if (oldPass.length>3) {
            PicPassController *picVc=(PicPassController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"PicPassController"];
            MyNavigationController *nav = [[MyNavigationController alloc]initWithRootViewController:picVc];
            _window.rootViewController=nav;
        }else{
            LoginViewController *Login = (LoginViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"LoginViewController"];
            MyNavigationController *nav = [[MyNavigationController alloc]initWithRootViewController:Login];
            _window.rootViewController=nav;
        }
    }
  [self autoLogin];

    return YES;
}

#pragma mark --- SVProgressHUD 偏好设置
- (void)svPreferrenceConf {
    
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD setBackgroundColor:[UIColor clearColor]];
    [SVProgressHUD setBackgroundLayerColor:[UIColor clearColor]];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD setMinimumDismissTimeInterval:CGFLOAT_MAX];
    
    
    UIImageView *svImgView = [[SVProgressHUD sharedView] valueForKey:@"imageView"];
    svImgView.contentMode = UIViewContentModeScaleAspectFit;
    CGRect imgFrame = svImgView.frame;
    
    // 设置图片的显示大小
    imgFrame.size = CGSizeMake(100, 80);
    svImgView.frame = imgFrame;
    NSMutableArray *images=[[NSMutableArray alloc] initWithCapacity:0];
    for (int i=1; i<13; i++) {
        [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];
    }

    svImgView.animationImages = images;
    svImgView.animationDuration = 1.75;
    svImgView.animationRepeatCount = 0;
    [svImgView startAnimating];
}

#pragma mark  自动登录
-(void)autoLogin{
    if ([UserModel isLogin]) {
        UserModel *mode=[UserModel getUser];
        NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"PASSCODE"];
        [HandleNetLogin CheckLoginForPhone:mode.Phone Password:password isShow:NO Success:^(NSDictionary *data) {
           
            NSMutableDictionary *infoData=[[NSMutableDictionary alloc] initWithDictionary:data];
            [infoData setObject:mode.Phone forKey:@"phone"];
            UserModel *model = [UserModel userInfo:infoData];
            [model save];
            
        } Failure:^(NSString *errorInfo) {
            
            [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"PICPASSCODE"];
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"USERISLOGINKEY"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            LoginViewController *Login = (LoginViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"LoginViewController"];
            MyNavigationController *nav = [[MyNavigationController alloc]initWithRootViewController:Login];
            _window.rootViewController=nav;
        }];
        
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {

    [self CheckAPPVerson];
}

-(void)CheckAPPVerson{
    //    [HandleNetProduct updateAPPVersonSuccess:^(NSDictionary *data) {
    //
    //    } Failure:^(NSString *errorInfo) {
    //
    //    }];
    //    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/id984909671?mt=8"]];
    //    return;
    
    //AAAMethodName=UpdateAPPEditionURL&APPEdition=1.0&Device=IOS
    //AAAMethodName=SelectAPPEditionURL&Device=IOS
    
    [HandleNetProduct CheckAPPVersonSuccess:^(NSDictionary *data) {
        if ([data[@"data"] count] == 0) {
            
            return ;
        }
        
        NSMutableString *version = [NSMutableString stringWithFormat:@"%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
        NSInteger appNum = [version changeToInteger];
        
        
        NSArray *dda =data[@"data"];
        NSDictionary *dicct = dda[0];
        NSInteger maxNum = [dicct[@"APPEdition"] changeToInteger];
    
       
        NSLog(@"version---%d  getAPPversion--%d ",(int)appNum,(int)maxNum);
        if (maxNum>appNum) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"检测到新版本，是否更新？" message:nil delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否",nil];
            alert.callBack = ^(UIAlertView *alertView, NSUInteger buttonIndex){
                if (buttonIndex == 0) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/id984909671?mt=8"]];
                    
                }else{
                    if ([dicct[@"CompulsoryRenewal"] isEqualToString:@"是"]) {
                        exit(0);
                    }
                }
                
            };
            [alert show];
        }else{
            
        }
    } Failure:^(NSString *errorInfo) {
        
    }];
    
    
}
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
